package evaluation;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

public class step2 {
	public static HashMap<String, Integer> google;

	public static void main(String[] args) throws IOException {
		String gd = args[0];
		String kld = args[1];
		String rd = args[2];
		double d= 150;
//		recall(d, gd, rd);
//		recall(d,gd,kld);
		store(gd, 200.0);
		precision(d, rd);
		precision(d, kld);
	}

	private static void store(String gd, double num) throws IOException {
		// TODO Auto-generated method stub
		FileReader stdin = new FileReader(gd);
		BufferedReader bufin = new BufferedReader(stdin);
		String line;
		google = new HashMap<String, Integer>();

		int count = 1;
		while ((line = bufin.readLine()) != null && count <= num) {
			String[] sp = line.split("-");
			int id = sp[1].lastIndexOf(" ");
			//System.out.println(id);
			String key = sp[1].substring(1, id);
			// System.out.println(key);

			google.put(key, count);
			count++;
		}

	}

	private static void precision(double max, String kld) throws IOException {
		// TODO Auto-generated method stub
		FileReader stdin = new FileReader(kld);
		BufferedReader bufin = new BufferedReader(stdin);
		String line;
		double accuratenum = 0;
		int count = 1;
		while ((line = bufin.readLine()) != null && count <= max) {
			String[] sp = line.split("%");
			String key = sp[1];
			if (google.containsKey(key) && google.get(key) <= max) {
				accuratenum += 1.0;
			}
			count++;
		}
		double result = accuratenum / max;
		System.out.println("precision at " + max + "\t" + result);

	}

	private static void recall(double num, String gd, String kld)
			throws IOException {
		// TODO Auto-generated method stub
		FileReader stdin = new FileReader(gd);
		BufferedReader bufin = new BufferedReader(stdin);
		String line;
		HashSet<String> google = new HashSet<String>();
		int count = 1;
		while ((line = bufin.readLine()) != null && count <= num) {
			String[] sp = line.split("-");
			int id = sp[1].lastIndexOf(" ");
			String key = sp[1].substring(1, id);

			// System.out.println(key);
			google.add(key);
			count++;
		}
		FileReader stdin1 = new FileReader(kld);
		BufferedReader bufin1 = new BufferedReader(stdin1);
		count = 1;
		double accuratenum = 0.0;
		while ((line = bufin1.readLine()) != null && count <= num) {
			String[] sp = line.split("%");
			String key = sp[1];
			if (google.contains(key)) {
				accuratenum += 1.0;
			}
			count++;
		}
		double result = accuratenum / num;
		System.out.println("recall at" + " " + num + "\t" + result);

	}

}
